package com.fyzk.dialog;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.fyzk.bean.RechargeBean;
import com.fyzk.utils.DensityUtils;
import com.fyzk.utils.ImageUtils;
import com.fyzk.utils.QRCodeCodec;
import com.fyzk.utils.QRCodeUtil;
import com.fyzk.utils.ZXingUtils;
import com.tyjc.lottery.R;

import org.xutils.view.annotation.ViewInject;

import http.DataUtil.text.TextUtil;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * <p>
 *
 * @author 指尖沙漏
 * @mood
 * @description
 * @date 2019/6/10 17:13
 * </p>
 */
public class WxCodeDialog extends Dialog implements View.OnClickListener {
    private Activity mContext;
    private TextView mTvTitle;
    private TextView mobileTxt;
    private TextView wxTxt;
    private ImageView ivCode;
    private ImageView shareCodeUI_ivShareCode_holder;
    private ImageView cancel;
    private TextView submitTxt, submit_and_openwchat;
    private RechargeBean rechargeBean;
    private String titleText = "";
    private RelativeLayout holder_rl;

    public WxCodeDialog(Activity context, RechargeBean rechargeBean) {
        this(context, rechargeBean, "线下支付");
    }

    public WxCodeDialog(Activity context, RechargeBean rechargeBean, String title) {
        super(context, R.style.commonDialog);
        this.mContext = context;
        this.rechargeBean = rechargeBean;
        this.titleText = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_wx_code);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        mTvTitle = findViewById(R.id.tv_title);
        mobileTxt = findViewById(R.id.mobile);
        holder_rl = findViewById(R.id.holder_rl);
        wxTxt = findViewById(R.id.wx);
        ivCode = findViewById(R.id.code);
        cancel = findViewById(R.id.cancel);
        submitTxt = findViewById(R.id.submit);
        shareCodeUI_ivShareCode_holder = findViewById(R.id.shareCodeUI_ivShareCode_holder);
        submit_and_openwchat = findViewById(R.id.submit_and_openwchat);
        submitTxt.setOnClickListener(this);
        submit_and_openwchat.setOnClickListener(this);
        cancel.setOnClickListener(this);

        mTvTitle.setText(titleText);
        if (TextUtil.isEmpty(titleText)) {
            mTvTitle.setVisibility(View.GONE);
            setViewGravity(mobileTxt);
            setViewGravity(wxTxt);
        }

        if (rechargeBean != null) {
            mobileTxt.setText("客服电话：" + rechargeBean.getPhone());
            wxTxt.setText("微信号：" + rechargeBean.getWx());
//            bitmap = QRCodeUtil.createQRImage(rechargeBean.getImage(), DensityUtils.dip2px(mContext, 220), DensityUtils.dip2px(mContext, 220), BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.ic_launcher));
            //创建带logo的二维码图片,返回Bitmap
            //实例化QRCode编解码器
            QRCodeCodec  qrCodeCodec = new QRCodeCodec();
            bitmap = qrCodeCodec.encodeQRCode(rechargeBean.getImage(),DensityUtils.dip2px(mContext, 220), BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.ic_launcher)); //参数：要创建的二维码的内容、尺寸和logo
            if (bitmap == null) return;
            ivCode.setImageBitmap(bitmap);
            shareCodeUI_ivShareCode_holder.setImageBitmap(bitmap);
        }
    }

    Bitmap bitmap;

    private void setViewGravity(View view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (params instanceof LinearLayout.LayoutParams) {
            ((LinearLayout.LayoutParams) params).gravity = Gravity.CENTER_HORIZONTAL;
            view.setLayoutParams(params);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit:
                request();
                break;
            case R.id.submit_and_openwchat:
                requestAndOpenWx();
                break;
            case R.id.cancel:
                dismiss();
                break;
        }
    }

    private static final String[] STORAGE = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE};

    private void request() {
        if (!EasyPermissions.hasPermissions(mContext, STORAGE)) {
            AppSettingsDialog.Builder builder = new AppSettingsDialog.Builder(mContext);
            builder.setTitle("权限申请");
            builder.setRationale("当前应用缺少必要权限。\n\n请在手机设置中开启所需权限，以便正常使用");
            AppSettingsDialog build = builder.build();
            build.show();
        } else {
            ImageUtils.saveImageToGallery(getContext(), createBitmap(holder_rl));
        }
    }

    private void requestAndOpenWx() {
        if (!EasyPermissions.hasPermissions(mContext, STORAGE)) {
            AppSettingsDialog.Builder builder = new AppSettingsDialog.Builder(mContext);
            builder.setTitle("权限申请");
            builder.setRationale("当前应用缺少必要权限。\n\n请在手机设置中开启所需权限，以便正常使用");
            AppSettingsDialog build = builder.build();
            build.show();
        } else {
            ImageUtils.saveImageToGallery(getContext(), createBitmap(holder_rl));
            String url="weixin://";
            mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    }

    private Bitmap createBitmap(View view) {
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }
}
